package cn.taokuaidi.wangwang.client.mvc;

import cn.taokuaidi.wangwang.client.constants.AppEvents;
import cn.taokuaidi.wangwang.client.smart.AppConfig;
import cn.taokuaidi.wangwang.client.util.Screen;
import cn.taokuaidi.wangwang.client.widget.BaseEventCenter;
import cn.taokuaidi.wangwang.client.widget.grid.BuyerGrid;
import cn.taokuaidi.wangwang.client.widget.toolbar.BuyerPagingToolBar;
import cn.taokuaidi.wangwang.client.widget.toolbar.TkdPagingToolBar;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;

public class BuyerView extends LayoutContainer {

    private BuyerGrid        grid;
    private TkdPagingToolBar toolBar;

    @Override
    protected void onRender(com.google.gwt.user.client.Element parent, int index) {
        super.onRender(parent, index);
        init();
    }

    private void loadData() {
    }

    private void init() {
        Dispatcher.get().addController(new BaseEventCenter() {

            @Override
            public void handleEvent(AppEvent<?> event) {
                if (event.type == AppEvents.BUYER_DATA_LOADED) {
                    loadData();
                }
            }
        });
        // 初始化grid并获得数据
        grid = new BuyerGrid(null, null);
        grid.initData();
        AppConfig appConfig = Screen.getAppConfig();

        // 关联分页组件
        toolBar = new BuyerPagingToolBar(appConfig.buyerGridPageSize);

        ContentPanel panel = new ContentPanel();
        panel.setFrame(true);
        panel.setCollapsible(true);
        panel.setAnimCollapse(false);
        panel.setButtonAlign(HorizontalAlignment.CENTER);
        panel.setIconStyle("icon-table");
        panel.setHeading("Paging Grid");
        panel.setLayout(new FitLayout());
        panel.add(grid);
        panel.setHeaderVisible(false);
        panel.setScrollMode(Scroll.AUTO);
        panel.setSize(appConfig.buyerGridWidth, appConfig.buyerGridHeight);
        panel.setTopComponent(toolBar);
        add(panel);

    }
}
